package com.tarena.lbs.common.basic.converter;

import com.alibaba.fastjson2.JSONObject;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.dos.UserGroupDO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.vo.UserGroupVO;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
@Slf4j
public class UserGroupConverter {

    public UserGroupVO bo2vo(UserGroupBO bo){
        if (null == bo){
            return null;
        }
        UserGroupVO UserGroupVO = new UserGroupVO();
        BeanUtils.copyProperties(bo,UserGroupVO);
        return UserGroupVO;
    }

    public UserGroupDO param2DO(UserGroupParam param){
        if (null == param){
            return null;
        }
        UserGroupDO userGroupDO = new UserGroupDO();
        BeanUtils.copyProperties(param,userGroupDO);
        userGroupDO.setCreateAt(LocalDateTime.now());
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            log.info("UserGroupConverter loginUser = {}", JSONObject.toJSONString(loginUser));
            userGroupDO.setBusinessId(loginUser.getId().intValue());
        }
        return userGroupDO;
    }

    public UserGroupBO do2bo(UserGroupDO UserGroupDO) {
        if (null == UserGroupDO){
            return null;
        }
        UserGroupBO UserGroupBO = new UserGroupBO();
        BeanUtils.copyProperties(UserGroupDO,UserGroupBO);
        return UserGroupBO;
    }


    public List<UserGroupBO> doList2boList(List<UserGroupDO> doList ){
        List<UserGroupBO> boList = new ArrayList<>();
        if (CollectionUtils.isEmpty(doList)) {
            return boList;
        }
        for (UserGroupDO UserGroupDO : doList) {
            UserGroupBO UserGroupBO = new UserGroupBO();
            BeanUtils.copyProperties(UserGroupDO,UserGroupBO);
            boList.add(UserGroupBO);
        }
        return boList;
    }

    public List<UserGroupVO> boList2VoList(List<UserGroupBO> boList ){
        List<UserGroupVO> userGroupVoList = new ArrayList<>();
        if (CollectionUtils.isEmpty(boList)) {
            return userGroupVoList;
        }
        for (UserGroupBO UserGroupBO : boList) {
            UserGroupVO UserGroupVO = new UserGroupVO();
            BeanUtils.copyProperties(UserGroupBO,UserGroupVO);
            userGroupVoList.add(UserGroupVO);
        }
        return userGroupVoList;
    }

    public PageResult<UserGroupVO> assemblePageVOS(BasePageList<UserGroupBO> bos, BasePageQuery query) {
        if (bos == null || bos.getTotal() == 0) {
            return null;
        }
        PageResult<UserGroupVO> result = new PageResult<>();
        List<UserGroupVO> vos = new ArrayList<>();
        for (UserGroupBO temp : bos.getList()) {
            UserGroupVO vo = new UserGroupVO();
            BeanUtils.copyProperties(temp,vo);
            vos.add(vo);
        }
        result.setTotal(bos.getTotal());
        result.setObjects(vos);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());
        return result;
    }


}
